package com.nbclass.vo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * @标题 TreeVo
 * @作者 Mr.chen
 * @创建日期 2019/5/22 8:52
 * @版本号 1.0
 **/

public class TreeVo {
    private Integer id;
    private String name;
    private Integer pId;
    private List<TreeVo> children;

    public TreeVo() {
    }

    public TreeVo(Integer id, String name, Integer pId, List<TreeVo> children) {
        this.id = id;
        this.name = name;
        this.pId = pId;
        this.children = children;
    }

    public TreeVo(Integer id, String name, Integer pId) {
        this.id = id;
        this.name = name;
        this.pId = pId;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getpId() {
        return pId;
    }

    public void setpId(Integer pId) {
        this.pId = pId;
    }

    public List<TreeVo> getChildren() {
        return children;
    }

    public void setChildren(List<TreeVo> children) {
        this.children = children;
    }

    public static void main(String[] args) {
        List<TreeVo> allList = new ArrayList<>();
        List<TreeVo> topList = allList.stream()
                .filter(a->a.getpId()==0)
                .collect(toList());
        recursionChildren(topList,allList);
    }
    public static void recursionChildren(List<TreeVo> topList,List<TreeVo> allList){
        topList.forEach(t->{
            List<TreeVo> children = allList.stream()
                    .filter(node -> t.getId() == node.getpId())
                    .collect(toList());
            if(!children.isEmpty()){
                t.setChildren(children);
                recursionChildren(children,allList);
            }
        });
    }
}
